# Basic Http Binding Web Service Library
# for use with Robot Framework 
#
# ARobot 2012
#

import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)

import base64

from suds.client import Client
from suds import WebFault
from suds.sudsobject import Object
from suds.xsd.doctor import Import, ImportDoctor




class BasicHttpBindingWSLibrary:
    """ This is a library primarily meant for testing the Haikom BasicHttpBinding web service.
		It's using the SUDS web service client for Python (https://fedorahosted.org/suds/)
	"""
        
    def __init__(self, url):

        self.client = Client(url)
        
        # Force client to use provided url instead of the one published in WSDL
        self.client.set_options(location=url.rstrip('?wsdl'))
        

    #
    # Helper keywords
    #
    
    def CreatehkServiceIdObject(self, UserName, Password, CustomerId, ProjectName, StorageLevel):
        """ Creates, and returns a hkServiceId object (that is needed for all services)
        """
        hkServiceId = self.client.factory.create('hkServiceId')
        hkServiceId.CustomerId = CustomerId
        hkServiceId.Password = Password
        hkServiceId.ProjectName = ProjectName
	hkServiceId.StorageLevel = StorageLevel
	hkServiceId.UserName = UserName

                        
        return hkServiceId

    def CreatefiltersObject(self, Name, Operator, Value):
        """ Creates, and returns a filters object (that is needed for all services)
        """
        filters = self.client.factory.create('filters')

	XHkSearchFilter = self.client.factory.create('ns0:XHkSearchFilter')
        XHkSearchFilter.Name = Name
        XHkSearchFilter.Operator = Operator
	XHkSearchFilter.Value = Value

	filters.XHkSearchFilter = XHkSearchFilter
                        
        return filters
    

    #
    # Keywords exposing webservice methods
    #


    def GetAllPriceItems(self, hkServiceId, itemProvider):
        """ Invokes the GetAllPriceItems WS method of BasicHttpBindingWSInterface

        Takes the same arguments as input as the web service does, i.e. "hkServiceId" and "itemProvider".
        Use keyword "CreatehkServiceIdObject" to create a hkServiceId object.
	Use keyword "CreatefiltersObject" to create a filters object.
        """

        response = self.client.service.GetAllPriceItems(hkServiceId, itemProvider)

        # Print the sent SOAP message to Robot Framework log
        print self.client.last_sent()

        print response
        return response


